package com.freesky.biz.icbc.controller;

import com.alibaba.fastjson2.JSON;
import com.freesky.biz.icbc.domain.TFirstCustomer;
import com.freesky.biz.icbc.domain.TFirstCustomerConfig;
import com.freesky.biz.icbc.domain.TFirstCustomerVip;
import com.freesky.biz.icbc.model.IcbcEquityQryResponseV1;
import com.freesky.biz.icbc.service.IFirstCustomerService;
import com.freesky.biz.icbc.service.IcbcEquityService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.framework.web.domain.server.Sys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新客礼
 */
@RestController
@RequestMapping("/icbc/firstcustomer")
public class FirstCustomerController extends BaseController {

    @Autowired
    private IFirstCustomerService firstCustomerService;

    @Autowired
    private IcbcEquityService icbcEquityService;

    /**
     * 新客礼列表
     */
    @PreAuthorize("@ss.hasPermi('icbc:firstcustomer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TFirstCustomer firstCustomer) {
        startPage();
        List<TFirstCustomer> list = firstCustomerService.findList(firstCustomer);
        return getDataTable(list);
    }

    @Log(title = "新客礼", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('icbc:firstcustomer:export')")
    @GetMapping("/export")
    public AjaxResult export(TFirstCustomer firstCustomer,
                             HttpServletResponse response) {
        System.out.println(JSON.toJSONString(firstCustomer));
        return firstCustomerService.exportFile(firstCustomer);
    }

    /**
     * 新客礼列表
     */
    @PreAuthorize("@ss.hasPermi('icbc:firstcustomer:list')")
    @GetMapping("/listVip")
    public TableDataInfo listVip(TFirstCustomerVip firstCustomer) {
        startPage();
        List<TFirstCustomerVip> list = firstCustomerService.selectVipList(firstCustomer);
        return getDataTable(list);
    }

    @Log(title = "新客礼", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('icbc:firstcustomer:export')")
    @GetMapping("/exportVip")
    public AjaxResult exportVip(TFirstCustomerVip firstCustomer,
                             HttpServletResponse response) {
        System.out.println(JSON.toJSONString(firstCustomer));
        return firstCustomerService.exportVipFile(firstCustomer);
    }

    /**
     * 活动查询
     */
    @PreAuthorize("@ss.hasPermi('icbc:firstcustomer:list')")
    @GetMapping("/activityQuery")
    public AjaxResult activityQuery(String mobile) {

        List<String> activityList = new ArrayList<>();
        AjaxResult equityResult = icbcEquityService.equityQry(mobile);
        if (!equityResult.isSuccess()) {
            return equityResult;
        }

        Map<String, Object> respParam = (Map<String, Object>) (equityResult.get("body"));
        //非目标客群客户，详情请联系客户经理
        if ("1".equalsIgnoreCase(respParam.get("applyFlag").toString())) {
            return AjaxResult.error("用户无可参加活动!");
        }

        HashMap<String, IcbcEquityQryResponseV1.IcbcEquityQryResponseV1Body> activityMap
                = (HashMap<String, IcbcEquityQryResponseV1.IcbcEquityQryResponseV1Body>) respParam.get("activityMap");
        for (Map.Entry<String, IcbcEquityQryResponseV1.IcbcEquityQryResponseV1Body> entry : activityMap.entrySet()) {
            IcbcEquityQryResponseV1.IcbcEquityQryResponseV1Body icbcEquityQryResponseV1Body = entry.getValue();
            activityList.add(icbcEquityQryResponseV1Body.getActivityId());
        }

        if(activityList.size()>0) {
            List<TFirstCustomerConfig> list = firstCustomerService.selectFirstCustomerConfig(activityList);
            return AjaxResult.success("成功", list);
        } else {
            return AjaxResult.success("成功", new ArrayList<>());
        }
    }
}
